package com.yunxi.module.system.controller.admin.tenant;

import com.yunxi.framework.common.pojo.CommonResult;
import com.yunxi.framework.common.pojo.PageParam;
import com.yunxi.framework.common.pojo.PageResult;
import com.yunxi.framework.common.util.object.BeanUtils;
import com.yunxi.framework.excel.util.ExcelUtils;
import com.yunxi.module.system.model.tenant.TenantDO;
import com.yunxi.module.system.service.tenant.TenantService;
import com.yunxi.module.system.vo.admin.tenant.tenant.TenantPageReqVO;
import com.yunxi.module.system.vo.admin.tenant.tenant.TenantRespVO;
import com.yunxi.module.system.vo.admin.tenant.tenant.TenantSaveReqVO;
import com.yunxi.module.system.vo.admin.tenant.tenant.TenantSimpleRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 管理后台 - 租户
 * </p>
 *
 * @author lidy
 * @since 2024-05-09
 */
@Tag(name = "管理后台 - 租户")
@RestController
@RequestMapping("/system/tenant")
public class TenantController {

    @Resource
    private TenantService tenantService;

    @GetMapping("/exportTenant")
    @Operation(summary = "导出租户信息")
    @PreAuthorize("@ss.hasPermission('system:tenant:export')")
    public void exportTenant(HttpServletResponse response, @Valid TenantPageReqVO reqVO) throws IOException {
        reqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        // 查询数据
        List<TenantDO> list = tenantService.getTenantPage(reqVO).getList();
        // 导出
        ExcelUtils.write(response, "租户列表.xlsx", "租户信息",
                TenantRespVO.class, BeanUtils.toBean(list, TenantRespVO.class));
    }

    @GetMapping("/getByWebsite")
    @Operation(summary = "根据租户域名,获得租户信息", description = "登录界面，根据用户的域名，获得租户信息")
    @PermitAll
    @Parameter(name = "website", description = "租户域名", required = true, example = "www.yunxiit.cn")
    public CommonResult<TenantSimpleRespVO> getTenantByWebsite(@RequestParam("website") String website) {
        TenantDO tenantDO = tenantService.getTenantByWebsite(website);
        return CommonResult.success(BeanUtils.toBean(tenantDO, TenantSimpleRespVO.class));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除租户")
    @PreAuthorize("@ss.hasPermission('system:delete')")
    @Parameter(name = "tenantId", description = "租户编号", required = true, example = "1024")
    public CommonResult<Boolean> deleteTenant(@RequestParam("tenantId") Long tenantId) {
        tenantService.deleteTenant(tenantId);
        return CommonResult.success(true);
    }

    @PostMapping("/create")
    @Operation(summary = "创建租户")
    @PreAuthorize("@ss.hasPermission('system:tenant:create')")
    public CommonResult<Long> createTenant(@Valid @RequestBody TenantSaveReqVO createReqVO) {
        Long tenantId = tenantService.createTenant(createReqVO);
        return CommonResult.success(tenantId);
    }

    @PutMapping("/update")
    @Operation(summary = "更新租户")
    @PreAuthorize("@ss.hasPermission('system:tenant:update')")
    public CommonResult<Boolean> updateTenant(@Valid @RequestBody TenantSaveReqVO updateReqVO) {
        tenantService.updateTenant((updateReqVO));
        return CommonResult.success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得租户")
    @Parameter(name = "tenantId", description = "租户编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:tenant:query')")
    public CommonResult<TenantRespVO> getTenant(@RequestParam("tenantId") Long tenantId) {
        TenantDO tenant = tenantService.getTenant(tenantId);
        return CommonResult.success(BeanUtils.toBean(tenant, TenantRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得租户分页列表")
    @PreAuthorize("@ss.hasPermission('system:tenant:query')")
    public CommonResult<PageResult<TenantRespVO>> getTenantPage(@Valid TenantPageReqVO reqVO) {
        PageResult<TenantDO> pageResult = tenantService.getTenantPage(reqVO);
        return CommonResult.success(BeanUtils.toBean(pageResult, TenantRespVO.class));
    }

    @GetMapping("/getIdByName")
    @PermitAll
    @Operation(summary = "使用租户名，获得租户编码", description = "登录界面，根据用户的用户名，获得租户编号")
    @Parameter(name = "name", description = "租户名", required = true, example = "1024")
    public CommonResult<Long> getTenantIdByName(@RequestParam("name") String name) {
        TenantDO tenantDO = tenantService.getTenantByName(name);
        return CommonResult.success(tenantDO != null ? tenantDO.getId() : null);
    }

}
